<?php

/**
 * Admin_Monitor_Cpu Model
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
* @category Admin_Monitor_Memory_Models
 * @package  Admin_Monitor_Memory_Package
 * @author   Johari <joharijumali@gmail.com>
 */

namespace Admin\Models\Monitor;

use \Laravel\Database\Eloquent\Model as Eloquent;

/**
 * Class Admin_Monitor_Cpu Model
 * 
 * Class Description
 *
 * @category Admin_Monitor_Cpu_Models
 * @package  Admin_Monitor_Cpu_Package
 * @author   
 * @license  Laravel License
 * @link     
 */

class Cpu extends Eloquent 
{

    public static $timestamps = true;
    public static $table = 'server_cpu';
    public static $key = 'cpu_id';


	/**
     * Function cron that pass parameter
     *
     * @param integer $id   the integer to quote
     * @param varchar $data the varchar to quote
     * @param varchar $date the varchar to quote
	 *
	 * @return parameter.
     */
    public static function cron($id, $data, $date)
	{	
        $data = json_decode($data);

        $exist = Cpu::where('server_id', '=', $id)->first(array('cpu_id'));

        $Cpu = (empty($exist))? new Cpu : Cpu::find($exist->cpu_id);

        $Cpu->server_id = $id;
        $Cpu->cpu_user = $data->user;
        $Cpu->cpu_nice = $data->nice;
        $Cpu->cpu_system = $data->system;
        $Cpu->cpu_iowait = $data->iowait;
        $Cpu->cpu_idle = $data->idle;
        $Cpu->cpu_steal = $data->steal;
        $Cpu->cpu_date = $date;
        $Cpu->save();

        Cpu::_stor($id, $data, $date, 'stor_cpu');
    }
	
	/**
     * Function stor that pass parameter
     *
     * @param integer $id    the integer to quote
     * @param varchar $data  the varchar to quote
     * @param varchar $date  the varchar to quote
     * @param varchar $table the varchar to quote
	 *
	 * @return parameter.
     */
    protected static function _stor($id, $data, $date, $table)
	{
        static::$table = $table;

        $Cpu = new Cpu;

        $Cpu->server_id = $id;
        $Cpu->cpu_user = $data->user;
        $Cpu->cpu_nice = $data->nice;
        $Cpu->cpu_system = $data->system;
        $Cpu->cpu_iowait = $data->iowait;
        $Cpu->cpu_idle = $data->idle;
        $Cpu->cpu_steal = $data->steal;
        $Cpu->cpu_date = $date;
        $Cpu->save();
    }

    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabSingleCpu($server_id)
    {
        
        $Cpu = Cpu::where('server_id', '=', $server_id)->first()->to_array();

        return $Cpu;
    }


    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabAllCpu()
    {
        
        $Cpu = Cpu::all();

        $data = array();

        foreach ($Cpu as $key => $value) {
            $data[$value->server_id] = $value->to_array();
            
            unset($data[$value->server_id]['created_at']);
            unset($data[$value->server_id]['updated_at']);
            unset($data[$value->server_id]['cpu_date']);
        }

        return $data;
    }



}
